package com.baoteng.rbspy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader;
import org.springframework.stereotype.Service;

import com.baoteng.rbspy.config.car.URLConfig;
import com.baoteng.rbspy.model.Model;
import com.baoteng.rbspy.model.kind.BIPO;
import com.baoteng.rbspy.model.kind.TaxQueryPO;
import com.baoteng.rbspy.model.request.RenewalDetailsRequest;
import com.baoteng.rbspy.model.request.TaxRequest;
import com.baoteng.rbspy.model.response.ResponseModel;
import com.baoteng.rbspy.service.KindService;
import com.baoteng.rbspy.service.OkHttpClientService;
import com.baoteng.rbspy.service.RenewalService;
import com.baoteng.rbspy.util.BeanConvertUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.samskivert.mustache.Mustache;

@Service
public class KindServiceImpl implements KindService {
	@Autowired
	OkHttpClientService okHttpClientService;

	@Autowired
	private Mustache.Compiler compiler;

	@Autowired
	MustacheResourceTemplateLoader mustacheTemplateLoader;

	@Autowired
	URLConfig urlConfig;

	@Autowired
	RenewalService renewalService;

	BeanConvertUtil beanUtil = BeanConvertUtil.newInstance();

	public BIPO renewalBIInfo(RenewalDetailsRequest request) {
		ResponseModel<BIPO> biResponse = okHttpClientService
				.getHtml(urlConfig.getBIUrl() + "?" + beanUtil.convert2RequestURLParam(request), BIPO.class);
		return biResponse.isSuccessful() && biResponse.getBody() != null ? biResponse.getBody() : null;
	}

	public TaxQueryPO queryTax(TaxRequest request) {
		ResponseModel<Model<TaxQueryPO>> response = okHttpClientService
				.getJson(urlConfig.getTaxQueryUrl() + request.genRequestURI(), new TypeReference<Model<TaxQueryPO>>() {});
		return response.isSuccessful() && response.getBody() != null
				? response.getBody().getData() != null && response.getBody().getData().size()>0? response.getBody().getData().get(0) : null
				: null;
	}
}
